package com.chang.seckill.controller;

import com.chang.common.utils.R;
import com.chang.seckill.service.SeckillService;
import com.chang.seckill.to.SeckillSkuRedisTo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: changzhenxi
 * @date: 2025年04月15日 15:21
 * @Version: 1.0
 * @description:
 */
@Controller
public class SeckillController {

    @Resource
    SeckillService seckillService;

    // 返回当前时间可以参与的秒杀商品信息
    @ResponseBody
    @GetMapping("getCurrentSeckillSku")
    public R getCurrentSeckillSku() {
        System.out.println("开始查询当前时间可以参与的秒杀商品信息......");
        List<SeckillSkuRedisTo> list = seckillService.getCurrentSeckillSku();
        return R.ok().setData(list);
    }

    @ResponseBody
    @GetMapping("sku/Seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo skuRedisTo = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(skuRedisTo);
    }

    @GetMapping("kill")
    public String seckill(@RequestParam("killId") String killSkuId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {

        String orderSn = seckillService.kill(killSkuId, key, num);

        model.addAttribute("orderSn", orderSn);
        return "success";
    }

}
